<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button icon="el-icon-plus" size="small" type="primary" @click="showDialog=true">新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="assignPerm(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="showEdit(scope.row.id)">编辑</el-button>
                  <!-- {{ scope.row.id }} -->
                  <el-button size="small" type="danger" @click="del(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="end" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <span>共{{ total }}条</span>
              <el-pagination
                layout="prev, pager, next"
                :page-size="pageConfig.pagesize"
                :total="total"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfoFrom.name" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfoFrom.companyAddress" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="电话">
                <el-input v-model="companyInfoFrom.companyPhone" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfoFrom.mailbox" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfoFrom.remarks" disabled style="width: 400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog :title="roleForm.id?'编辑角色':'新增角色'" :visible="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <el-dialog title="分配权限" :visible="isShowPermission" @close="btnPermCancel">
        <!-- data 是数据数组, 套着一堆对象 props 指定要显示的字段,
        default-expand-all 默认展开所有 -->
        <el-checkbox-group v-model="checkedPerm">
          <!-- 本来多个多选框需要自己 v-for 遍历, 但是这里由树形处理了 -->
          <el-tree
            :data="permissionList"
            :props="{label: 'name'}"
            default-expand-all
          >
            <!-- 每个属性节点都是一个多选框 -->
            <template v-slot="scope">
              <el-checkbox :label="scope.data.id">
                {{ scope.data.name }}
              </el-checkbox>
            </template>
          </el-tree>

        </el-checkbox-group>
        <template #footer>
          <el-button size="small" type="primary" @click="btnPermOK">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyInfoAPI, getRoleListAPI, getRoleDetailAPI, updateRoleDetailAPI, addRoleAPI, delRoleAPI, assignPerm } from '@/api/setting'
import { mapState } from 'vuex'
import { Message } from 'element-ui'
import { getPermissionList } from '@/api/permisson'
import { listToTree } from '@/utils/index'
export default {
  data() {
    return {
      companyInfoFrom: {},
      pageConfig: {
        page: 1, // 页码
        pagesize: 5 // 每页条目数
      },
      roleList: [],
      total: 0,
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      roleId: '',
      permissionList: [],
      checkedPerm: [],
      isShowPermission: false,
      rules: {
        name: [
          { required: true, message: '输入不为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '输入不为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  async created() {
    // const res = await getCompanyInfoAPI(this.userInfo.companyId)
    // console.log(res)
    // this.companyInfoFrom = res
    this.companyInfoFrom = await getCompanyInfoAPI(this.userInfo.companyId)
    // 渲染时获取权限列表信息
    const res = await getPermissionList()
    // 树形数据处理
    const treeRes = listToTree(res, '0')
    this.permissionList = treeRes
    this.getRoleList()
  },
  methods: {
    async assignPerm(id) {
      // 回显默认选中情况
      const res = await getRoleDetailAPI(id)
      // console.log(res)
      this.checkedPerm = res.permIds
      this.roleId = id
      this.isShowPermission = true
    },
    async btnPermOK() {
      // 更新权限请求
      await assignPerm({
        id: this.roleId,
        permIds: this.checkedPerm
      })
      this.$message.success('修改成功')
      this.isShowPermission = false
    },
    btnPermCancel() {
      this.isShowPermission = false
    },
    async getRoleList() {
      const resList = await getRoleListAPI(this.pageConfig)
      // console.log(resList)
      this.roleList = resList.rows
      this.total = resList.total
    },
    handleCurrentChange(pageNum) {
      // console.log(pageNum)
      this.pageConfig.page = pageNum
      this.getRoleList()
    },
    async showEdit(id) {
      this.roleForm = await getRoleDetailAPI(id)
      this.showDialog = true
    },
    btnCancel() {
      this.showDialog = false
      this.$refs.roleForm.clearValidate() // 清空之前的校验结果
      this.roleForm = {
        name: '',
        description: ''
      } // 手动重置表单
    },
    async btnOK() {
      // console.log(this.roleForm)
      try {
        await this.$refs.roleForm.validate() // 非空校验
        if (this.roleForm.id) { // 通过表单id 判断状态
          await updateRoleDetailAPI(this.roleForm)
        } else {
          await addRoleAPI(this.roleForm)
        }
        Message.success('操作成功')
        this.getRoleList()
        this.btnCancel()
      } catch (error) {
        console.log(error)
      }
    },
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRoleAPI(id)
        this.getRoleList()
        Message.success('删除成功')
      }).catch(() => {
        Message.info('取消删除')
      })
    }
  }
}
</script>

<style></style>
